package spynx.util;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.net.URI;

public class OutFile extends java.io.File {

	private static final long serialVersionUID = 1L;
	
	protected PrintStream out;

	public OutFile(String arg0) {
		super(arg0);
	}

	public OutFile(URI arg0) {
		super(arg0);
	}

	public OutFile(String arg0, String arg1) {
		super(arg0, arg1);
	}

	public OutFile(java.io.File arg0, String arg1) {
		super(arg0, arg1);
	}
	
	protected synchronized void openFile() throws FileNotFoundException {
		if (out == null)
			out = new PrintStream(this);
	}

	public synchronized void write(String template, Object...args) throws FileNotFoundException {
		openFile();
		if (args.length > 0)
			out.print(template);
		else
			out.printf(template, args);
	}
	
	public void write(Object value) throws FileNotFoundException {
		write(value != null ? value.toString() : "", new Object[] {});
	}
	
	public void writeln(String template, Object...args) throws FileNotFoundException {
		write(template + "\n", args);
	}
	
	public void writeln() throws FileNotFoundException {
		write("\n");
	}
	
	public void writeln(Object value) throws FileNotFoundException {
		write(value);
		write("\n");
	}
	
	public void close() {
		out.close();
	}
}
